﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Security.Cryptography;
using WebKpiPage.Data.Common;
namespace WebKpiPage.Data
{
    public static  class UserMgr
    {
         /// </summary>
        /// <param name="loginId">登陆用户名</param>
        /// <param name="password">密码</param>
        /// <param name="ret">登陆结果：0、成功；-1、数据访问失败；-2、用户名错误或不存在；-3、登录口令错误</param>
        /// <returns></returns>
        public static DataSet IsValid_InternalUser_v2(string loginId, string password, ref int ret)
        {
            byte[] result = Encoding.Default.GetBytes(password);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string pwd = BitConverter.ToString(output).Replace("-", "");
            SqlConnection conn = ConnMng.GetConnection();
            if (conn == null || conn.State != System.Data.ConnectionState.Open)
            {
                return null;
            }
            try
            {
                string commandText = "MTNOH_GZ_APP.dbo.mtsp_查询_登录用户信息";
                SqlParameter[] sp = new SqlParameter[1];
                sp[0] = new SqlParameter("@LoginID", loginId);
                SqlDataAdapter ad = SqlHelper.ExecuteDataAdapter(conn, CommandType.StoredProcedure, commandText, sp);
                ad.TableMappings.Add("Table", "用户信息");
                ad.TableMappings.Add("Table1", "用户所辖地市");
                ad.TableMappings.Add("Table2", "用户权限");
                DataSet ds = new DataSet();
                ad.Fill(ds);

                DataTable dtUser = ds.Tables["用户信息"];
              //  DataTable dtUserCity = ds.Tables["用户所辖地市"];
             //   DataTable dtUserPri = ds.Tables["用户权限"];
                if (dtUser == null)
                {
                    ret = -1;
                }
                else if (dtUser.Rows.Count == 0)
                {
                    ret = -2;
                }
                else
                {
                    if (pwd != dtUser.Rows[0]["Password"].ToString())
                    {
                        ret = -3;
                    }
                }
                return ds;
            }
            catch (Exception)
            {
                ret = -1;
                return null;
            }
            finally
            {
                ConnMng.ReleaseConnection(conn);
            }
        }

     
        public static DataTable GetUserInfoByUserId(Guid userId)
        {
            SqlConnection conn = ConnMng.GetConnection();
            if (conn == null || conn.State != System.Data.ConnectionState.Open)
            {
                return null;
            }
            try
            {
                string commandText = "MTNOH_GZ_APP.dbo.mtsp_查询_根据userID查找同组人员";
                SqlParameter[] sp = new SqlParameter[1];
                sp[0] = new SqlParameter("@ID", userId);
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, commandText, sp);
                return ds.Tables[0];
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnMng.ReleaseConnection(conn);
            }
        }
        
       
    }
}
